package org.example.cloudalibaba.controller;

import cn.hutool.core.util.IdUtil;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.example.cloudalibaba.myhandler.CustomBlockHandler;
import org.example.springcloud.common.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class RateLimitController {

    // 流控规则 资源名：byResource
    @GetMapping("byResource")
    @SentinelResource(value = "byResource",
            blockHandlerClass = CustomBlockHandler.class, // 指定自定义的限流处理类（默认为本类）
            blockHandler = "resourceHandleBlock") // 指定自定义的限流处理方法
    public Result<String> byResource() {
        return Result.success("byResource: {}", IdUtil.fastSimpleUUID());
    }

    // 流控规则 资源名：/rateLimit/byUrl （注意斜杠）
    @GetMapping("rateLimit/byUrl")
    @SentinelResource(
            value = "/rateLimit/byUrl", // 此处的value值为Sentinel控制台中的sentinel_default_context，不配则默认是全类名+方法名()
            blockHandlerClass = CustomBlockHandler.class, // 指定自定义的限流处理类（默认为本类）
            blockHandler = "urlHandleBlock") // 指定自定义的限流处理方法
    public Result<String> byUrl() {
        return Result.success("byUrl: {}", IdUtil.fastSimpleUUID());
    }

}
